package com.lanou.connotationjoke.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TabHost;

import com.android.volley.toolbox.NetworkImageView;
import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.activity.LoginActivity;
import com.lanou.connotationjoke.entity.LoginUserEty;
import com.lanou.connotationjoke.tool.VolleySingleton;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * Created by dllo on 16/1/8.
 */
public class DiscoveryFragment extends BaseFragment implements View.OnClickListener {
    private TabHost tabHost;
    private NetworkImageView loginIv;
    private ImageView searchIv;
    private SharedPreferences sharedPreferences;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_discovery,null);
        tabHost = (TabHost) view.findViewById(android.R.id.tabhost);
        loginIv = (NetworkImageView) view.findViewById(R.id.iv_discovery_to_login);
        searchIv = (ImageView) view.findViewById(R.id.iv_discovery_search);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        EventBus.getDefault().register(this);
        sharedPreferences = getActivity().getSharedPreferences("userData", Context.MODE_PRIVATE);
        String iconUrl = sharedPreferences.getString("icon","");
        loginIv.setDefaultImageResId(R.mipmap.ic_discovery_default_avatar);
        loginIv.setErrorImageResId(R.mipmap.ic_discovery_default_avatar);
        loginIv.setImageUrl(iconUrl, VolleySingleton.getImageLoader());


        tabHost.setup();
        tabHost.addTab(tabHost.newTabSpec("tab_11")
                .setIndicator(getActivity().getLayoutInflater().inflate(R.layout.tab_discovery_hot, null))
                .setContent(R.id.tab_11));
        tabHost.addTab(tabHost.newTabSpec("tab_22")
                .setIndicator(getActivity().getLayoutInflater().inflate(R.layout.tab_discovery_friends, null))
                .setContent(R.id.tab_22));
        FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.tab_11, new DiscoveryHotFragment());
        transaction.replace(R.id.tab_22, new DiscoveryFriendsFragment());
        transaction.commit();
        //注册标题上按键的监听
        loginIv.setOnClickListener(this);
        searchIv.setOnClickListener(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_discovery_to_login:
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_discovery_search:
                break;
        }
    }


    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onMainEvent(LoginUserEty loginUserEty) {
        loginIv.setImageUrl(loginUserEty.getIconUrl(), VolleySingleton.getImageLoader());
    }

}
